﻿// Copyright (c) 2014 Converter Systems LLC

using ConverterSystems.ServiceModel;
using ConverterSystems.Workstation.Infrastructure;
using Opc.Ua.Client;
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;

namespace ConverterSystems.Workstation.ViewModels
{
    /// <summary>
    /// A view model for DetailView.
    /// </summary>
    [Export]
    public class DetailViewModel : NavigationViewModelBase
    {
        private TestServerSession _session;
        private Subscription _subscription;

        [ImportingConstructor]
        public DetailViewModel(TestServerSession session)
        {
            if (session == null) throw new ArgumentNullException("session");
            _session = session;
        }

        public override void OnNavigatedTo(Microsoft.Practices.Prism.Regions.NavigationContext navigationContext)
        {
            if (_subscription != null) return;
            _subscription = new Subscription(_session.DefaultSubscription);
            _subscription.WithModel(this).AddBindings();
            _session.AddSubscription(_subscription);
        }

        public override void OnNavigatedFrom(Microsoft.Practices.Prism.Regions.NavigationContext navigationContext)
        {
            if (_subscription == null) return;
            _session.RemoveSubscription(_subscription);
            _subscription.Dispose();
            _subscription = null;
        }

        // add properties here
    }
}